name: MindsDB Releases

on:
  release:
    types: [published]
    paths-ignore:
      - 'docs/**'
      - 'README.md'

jobs:
  check-version:
    runs-on: [self-hosted, dev]
    if: github.actor != 'mindsdbadmin'
    steps:
    - uses: actions/checkout@v3.5.3
    # Get clean environment variables via https://github.com/marketplace/actions/github-environment-variables-action
    - uses: FranzDiebold/github-env-vars-action@v2
    - name: Set up Python
      uses: actions/setup-python@v4.7.0
      with:
        python-version: ${{ vars.CI_PYTHON_VERSION }}
    - name: Check Version
      run: |
        PYTHONPATH=./ python tests/scripts/check_version.py ${{ env.CI_REF_NAME }} ${{ github.event.release.prerelease }}

  deploy_to_pypi:
    runs-on: [self-hosted, dev]
    needs: check-version
    if: github.actor != 'mindsdbadmin'
    steps:
    - uses: actions/checkout@v3.5.3
    - name: Set up Python
      uses: actions/setup-python@v4.7.0
      with:
        python-version: ${{ vars.CI_PYTHON_VERSION }}
    - name: Install dependencies
      run: |
        pip install -r requirements/requirements-dev.txt
    - name: Build and publish
      env:
        TWINE_USERNAME:  __token__
        TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
      run: |
        # This uses the version string from __about__.py, which we checked matches the git tag above
        python setup.py sdist
        twine upload dist/*

  docker_build:
    # Build our docker images based on our bake file
    # This will tag with the release version tag and push to both dockerhub and ECR
    runs-on: [self-hosted, dev]
    needs: [check-version]
    if: github.actor != 'mindsdbadmin'
    steps:
      - uses: actions/checkout@v2
      - name: Docker Login
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      # Build the bakefile and push
      - uses: ./.github/actions/docker-bake
        with:
          push-to-dockerhub: true


  trigger_deploy:
    # Trigger private repo to deploy to prod env
    runs-on: [self-hosted, dev]
    needs: docker_build
    if: github.actor != 'mindsdbadmin'
    environment:
      name: prod
    steps:
      - uses: FranzDiebold/github-env-vars-action@v2
      - uses: convictional/trigger-workflow-and-wait@v1.6.5
        with:
          owner: mindsdb
          repo: INTERNAL-mindsdb-build-deploy-to-kubernetes
          github_token: ${{ secrets.REPO_DISPATCH_PAT_TOKEN }}
          workflow_file_name: deploy-prod.yml
          ref: master
          client_payload: '{"image-tag-prefix": "${{ env.CI_REF_NAME }}", "deploy-env": "prod"}'
